import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;


public class CustomFieldDialog extends Dialog implements ActionListener,WindowListener
	{

	private Button buttonOK=new Button("OK");
	private Button buttonCancel=new Button("Cancel");

	private final Label lableWidth = new Label("Height");
	private final Label lableHeight = new Label("Width");
	private final Label lableNumberOfMines=new Label("Mines");

	private TextField textWidth;
	private TextField textHeight;
	private TextField textNumberOfMines;


	private int width;
	private int height;
	private int numberOfMines;

	private int defaultWidth;
	private int defaultHeight;
	private int defaultNumberOfMines ;

	private Frame owner;


	public CustomFieldDialog(Frame owner,int defaultWidth,int defaultHeight,
															int defaultMines){

		
		super(owner,"Custom Field",true);

		this.owner = owner;

		textWidth = new TextField(""+defaultWidth);
		textHeight = new TextField(""+defaultHeight);
		textNumberOfMines = new TextField(""+defaultMines);

		this.defaultWidth = defaultWidth;
		this.defaultHeight = defaultHeight;
		this.defaultNumberOfMines = defaultMines;

		Panel sp=new Panel();
		Panel pl=new Panel();
		Panel pr=new Panel();


		GridBagLayout  splayout=new GridBagLayout();
		GridBagConstraints gbc=new GridBagConstraints();

		sp.setLayout(splayout);
		//  sp.setBackground(Color.blue);
		pl.setLayout(new GridLayout(3,1));
		//  pl.setBackground(Color.green);
		pr.setLayout(new GridLayout(2,0,10,10));
		//  pr.setBackground(Color.red);

		//  GridBagConstraints gbc=new GridBagConstraints();
		gbc.weightx+=10;
		gbc.weighty+=10;
		gbc.gridwidth=GridBagConstraints.RELATIVE;

		setResizable(false);
		pl.add(lableWidth);
		pl.add(textWidth);
		pl.add(lableHeight);
		pl.add(textHeight);
		pl.add(lableNumberOfMines);
		pl.add(textNumberOfMines);
		pr.add(buttonOK);
		pr.add(buttonCancel);
		splayout.setConstraints(pl,gbc);
		sp.add(pl);
		gbc.gridwidth=GridBagConstraints.REMAINDER;
		splayout.setConstraints(pr,gbc);

		sp.add(pr);

		// add(sp);
		// Panel tmpp=new Panel();
		//  tmpp.setLayout(new GridLayout(1,1,10,10));
		splayout=new GridBagLayout(); 
		// tmpp.add(sp);
		gbc=new GridBagConstraints();
		setLayout(splayout);
		gbc.ipadx+=20;
		gbc.ipady+=10;

		splayout.setConstraints(sp,gbc);
		add(sp);
		validate();
		pack();
		setLocation(owner.getLocation().x + owner.getSize().width/2 - getSize().width/2,
					owner.getLocation().y + owner.getSize().height/2 - getSize().height/2);
		setVisible(false);
		buttonOK.addActionListener(this);
		buttonCancel.addActionListener(this);
		addWindowListener(this);


	}


	public void setVisible(boolean visible)
	{

		if(visible)
		{
			setLocation(owner.getLocation().x + owner.getSize().width/2 - getSize().width/2,
						owner.getLocation().y + owner.getSize().height/2 - getSize().height/2);
			super.setVisible(visible);
		}

	}

	public int getWidth()
	{

		return width;

	}
	

	public int getHeight()
	{
		return height;
	}

	public int getNumberOfMines()
	{
		return numberOfMines;
	}


	//
	// ItemListener interface method implementation.
	//
	public void actionPerformed(ActionEvent e) 
	{

		if(e.getSource().equals(buttonOK))
		{

			
			try{
				width = Integer.parseInt(textWidth.getText());
			}catch(NumberFormatException nfe)
			{
				width = defaultWidth;
			}

			width = width > Game.MAX_WIDTH? Game.MAX_WIDTH:width;
			textWidth.setText(""+width);

			try{
				height= Integer.parseInt(textHeight.getText());
			}catch(NumberFormatException nfe)
			{
				height= defaultHeight;
			}
			height = height > Game.MAX_HEIGHT? Game.MAX_HEIGHT:height;

			textHeight.setText(""+height);

			try{
				numberOfMines = Integer.parseInt(textNumberOfMines.getText());
			}catch(NumberFormatException nfe)
			{
				numberOfMines = defaultNumberOfMines;
			}

			numberOfMines = numberOfMines > ( (width * height) - (width+ height -1))?
							( (width * height) - (width+ height -1)):numberOfMines;
			textNumberOfMines.setText(""+numberOfMines);

			super.setVisible(false);

		}else//since only the 2 bottons registered, it's an if/else...
		{

			super.setVisible(false);

		}

	}


	//---------------------------------
	//---------------------------------

	/**
	*	WindowListener interface implementation of windowOpened()
	**/

	public void windowOpened(WindowEvent e)
	{


	}


	/**
	*	WindowListener interface implementation of windowClosing()
	**/ 
	public void windowClosing(WindowEvent e)
	{

		//Hide the window.
		System.out.println("Window closing...");

	}

	/**
	*	WindowListener interface implementation of windowClosed()
	**/
	public void windowClosed(WindowEvent e)
	{


	}

	/**
	*	WindowListener interface implementation of windowIconified()
	**/
	public void windowIconified(WindowEvent e)
	{

	}


	/**
	*	WindowListener interface implementation of windowDeiconified()
	**/
	public void windowDeiconified(WindowEvent e)
	{


	}

	/**
	*	WindowListener interface implementation of windowActivated()
	**/
	public void windowActivated(WindowEvent e)
	{


	}

	/**
	*	WindowListener interface implementation of windowDeactivated()
	**/
	public void windowDeactivated(WindowEvent e)
	{


	}


}

